﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Account_Login : System.Web.UI.Page
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ListUserType();
        }

    }

    private void ListUserType()
    {
        DataSet dt = new DataSet();
        dt = BusinessClass.GetUserType();
        ddlRole.DataSource = dt;
        ddlRole.DataTextField = "Value";
        ddlRole.DataValueField = "IndexKey";
        ddlRole.DataBind();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        try
        {
            CallValidateUser();
        }
        catch (Exception ex)
        {
            lblError.Text = "Some Unexpected error occured please try again after some time <br/>" +
                "Error is : " + ex.Message;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void CallValidateUser()
    {
        BusinessClass bs = new BusinessClass();
        bs.UserName = txtUserName.Text.Trim();
        bs.Password = txtPassword.Text.Trim();
        bs.RoleId = Convert.ToInt32(ddlRole.SelectedValue);
        if (bs.IsValidUser())
        {
            Session["UserId"] = bs.UserId;
            Session["Role"] = bs.RoleId;
            Session["RoleId"] = bs.RoleId;
            Session["FullName"] = bs.FullName;
            //Server.Transfer("App_Pages/UserHome.aspx");
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            dvEror.Visible = true;
        }
    }
}
